//
//  NewfeatureCollectionViewController.swift
//  Weibo
//
//  Created by zxy on 2018/12/17.
//  Copyright © 2018 zxy. All rights reserved.
//

import UIKit
private let reuseIdentifier = "Cell"
var imageCount = 4
class NewfeatureCollectionViewController: UICollectionViewController {

    private var layout:UICollectionViewFlowLayout = NewFeatureLayout()
    
    init() {//设置layout
        super.init(collectionViewLayout: layout)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.register(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        collectionView.bounces = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.isPagingEnabled = true
    }


    // MARK: UICollectionViewDataSource

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageCount
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! NewFeatureCell
        cell.imageIndex = indexPath.item
        return cell
    }
    
    override func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        //indexPath.item:上一次的下标,所以这里不能用
        //拿到最后一个cell的indexPath
        let path = collectionView.indexPathsForVisibleItems.last
        //拿到最后一个cell
        let cell = collectionView.cellForItem(at: path!) as! NewFeatureCell
        if path!.item == imageCount - 1 {
            //开始动画
            cell.startAnimation()
        }
    }

}

//自定义UICollectionViewFlowLayout
private class NewFeatureLayout:UICollectionViewFlowLayout{
    override func prepare() {
        itemSize = UIScreen.main.bounds.size
        scrollDirection = .horizontal
        minimumLineSpacing = 0
        minimumInteritemSpacing = 0
    }
}



//自定义cell
 class NewFeatureCell: UICollectionViewCell{
    private lazy var featureImageView: UIImageView = {
        let imgView = UIImageView()
        imgView.frame = bounds
        return imgView
    }()
    
    //创建进入微博按钮
    private lazy var joinWeiBoBtn: UIButton = {
        let btn = UIButton(type: UIButton.ButtonType.custom);
        btn.setBackgroundImage(UIImage(named: "new_feature_button"), for: .normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_button_highlighted"), for: .highlighted)
        btn.isHidden = true
        contentView.addSubview(btn)
        btn.addTarget(self, action: #selector(joinWeiBoClick), for:.touchUpInside)
        return btn
    }()
    
    var imageIndex: Int?{
        didSet{
            featureImageView.image = UIImage(named: "new_feature_\(imageIndex! + 1)")
        }
    }
    
    //开始动画
    func startAnimation(){
        self.joinWeiBoBtn.transform = CGAffineTransform(scaleX: 0, y: 0)
        UIView.animate(withDuration: 2.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIView.AnimationOptions.init(rawValue: 0), animations: {
            //重置
            self.joinWeiBoBtn.transform = .identity
            self.joinWeiBoBtn.isHidden = false
        }) { (_) in
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(featureImageView)
        joinWeiBoBtn.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.contentView)
            make.bottom.equalTo(self.contentView).offset(-50)
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: - 进入微博
    @objc func joinWeiBoClick(){
        
        print(#function)
//        isLogin = true
        NotificationCenter.default.post(name: NSNotification.Name(XMGNotinationSwitchDefaultControllerKey), object: nil)
    }
}

